function saccade_triggered_act(act,frame_times,imeta_data,iframe_times)
% this function correlates pupil diameter and saccades with fluorescense
% activity

[imeta_data]=filter_eye_monitor_data(imeta_data);

%map act on iframe_times
for ind=1:length(iframe_times)
    [~,t_ind]=min(abs(frame_times-iframe_times(ind)));
    act_ift(ind)=act(t_ind);
end

pupil_diam=imeta_data(9,:);
pupil_vel=imeta_data(8,:);

if isnan(pupil_diam(1))
    pupil_diam(1)=0;
end

if isnan(pupil_vel(1))
    pupil_vel(1)=0;
end

while sum(isnan(pupil_diam))>0
    nan_ind=isnan(pupil_diam);
    pupil_diam(nan_ind)=pupil_diam(find(nan_ind)-1);
end

while sum(isnan(pupil_vel))>0
    nan_ind=isnan(pupil_vel);
    pupil_vel(nan_ind)=pupil_vel(find(nan_ind)-1);
end
    
pupil_vel=abs([0 diff(pupil_vel)]);
pupil_vel=double(pupil_vel>3);





figure;
subplot(211)
plot(xcorr(act_ift,pupil_diam,'unbiased'))
title('xcorr act - pupil diam.')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_diam),[yl(1) yl(2)],'k')

subplot(212)
plot(act_ift/max(act_ift))
hold on
plot(pupil_diam/max(pupil_diam),'r')

figure;
subplot(211)
plot(xcorr(act_ift,pupil_vel,'biased'))
title('xcorr act - saccades.')
axis tight
yl=ylim;
hold on
plot([1 1]*length(pupil_vel),[yl(1) yl(2)],'k')

subplot(212)
plot(act_ift/max(act_ift))
hold on
plot(pupil_vel/max(pupil_vel),'r')


